package com.tony.pmp.server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.base.Joiner;
import com.tony.pmp.common.utils.CommonUtil;
import com.tony.pmp.model.entity.SysUserRoleEntity;
import com.tony.pmp.model.mapper.SysUserRoleMapper;
import com.tony.pmp.server.service.SysUserRoleService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @Title: SysUserRoleServiceImpl
 * @Auther: 皮蛋布丁
 * @Date: 2021/07/25/16:38
 * @Description:
 */
@Service("sysUserRoleService")
public class SysUserRoleServiceImpl extends ServiceImpl<SysUserRoleMapper, SysUserRoleEntity> implements SysUserRoleService {

    /**
    * @Description: deleteBatch批量删除角色用户信息
    * @Param: [roleIds]
    * @return: boolean
    * @Author: 皮蛋布丁
    * @Date: 2021/7/25 16:47
    */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteBatch(List<Long> roleIds) {
        if (roleIds!=null && !roleIds.isEmpty()){
            String rolesIds= Joiner.on(",").join(roleIds);
            if (!baseMapper.deleteBatch(CommonUtil.concatStrToInt(rolesIds,","))) {
                return false;
            }
        }
        return true;
    }

    /**
    * @Description: saveOrUpdate 新增用户角色信息
    * @Param: [userId, roleIds]
    * @return: boolean
    * @Author: 皮蛋布丁
    * @Date: 2021/7/26 14:42
    */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveOrUpdate(Long userId, List<Long> roleIds) {
        //删除旧数据
        this.remove(new QueryWrapper<SysUserRoleEntity>().eq("user_id",userId));

        if (roleIds != null && !roleIds.isEmpty()) {
            SysUserRoleEntity entity;
            for (Long roleId : roleIds) {
                entity = new SysUserRoleEntity();
                entity.setRoleId(roleId);
                entity.setUserId(userId);
                this.save(entity);
            }
        }
        return true;
    }

    /**
    * @Description: queryRoleIdList 根据userId获取用户角色信息
    * @Param: [userId]
    * @return: java.util.List<java.lang.Long>
    * @Author: 皮蛋布丁
    * @Date: 2021/7/26 21:27
    */
    @Override
    public List<Long> queryRoleIdList(Long userId) {
        return baseMapper.queryRoleIdList(userId);
    }
}
